package com.example.springboot_system.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springboot_system.mapper.AnnounceMapper;
import com.example.springboot_system.pojo.Announce;
import com.example.springboot_system.service.AnnounceService;
import com.example.springboot_system.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class AnnounceServiceImpl implements AnnounceService {
    @Autowired(required = false)
    private AnnounceMapper announceMapper;

    @Override
    public ResponseResult findAnnounceById(String hiddenId) {
        QueryWrapper<Announce> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",hiddenId);
        Announce announce = announceMapper.selectOne(queryWrapper);
        if(announce==null){
            return new ResponseResult(201,"该条公告可能已被删除");
        }
        return new ResponseResult(200,announce);
    }

    @Override
    public ResponseResult findAllAnnounce(HttpSession session) {
        QueryWrapper<Announce> queryWrapper = new QueryWrapper<>();
        List<Announce> announceList = announceMapper.selectList(queryWrapper);
        session.setAttribute("announceList",announceList);
        return null;
    }

    @Override
    public ResponseResult updateAnnounceById(String hiddenId,String title,String contents, HttpSession session) {
        if(title==null || title==""){
            return new ResponseResult(201,"标题不能为空");
        }
        if(contents==null || contents==""){
            return new ResponseResult(202,"内容不能为空");
        }
        Announce announce = new Announce();
        announce.setId(hiddenId);
        announce.setTitle(title);
        announce.setContents(contents);
        int row = announceMapper.updateById(announce);
        if(row!=1){
            return new ResponseResult(203,"修改失败");
        }

        return new ResponseResult(200,"修改成功");
    }

    @Override
    public ResponseResult addAnnounce(String addTitle,String addContents, HttpSession session) {
        if(addTitle==null || addTitle==""){
            return new ResponseResult(201,"标题不能为空");
        }
        if(addContents==null || addContents==""){
            return new ResponseResult(202,"内容不能为空");
        }
        Announce announce = new Announce();
        String id = UUID.randomUUID().toString();
        announce.setId(id);
        announce.setTitle(addTitle);
        announce.setContents(addContents);
        Date time = new Date();
        announce.setCreateDatetime(time);
        announce.setAnnStatus(0);
        announce.setAnnDate(time);
        int row = announceMapper.insert(announce);
        if(row!=1){
            return new ResponseResult(203,"新增失败");
        }
        return new ResponseResult(200,"新增成功");
    }

    @Override
    public ResponseResult deleteAnnounceById(String hiddenId, HttpSession session) {
        QueryWrapper<Announce> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",hiddenId);
        int row = announceMapper.delete(queryWrapper);
        if(row!=1)
        {
            return new ResponseResult(201,"删除失败");
        }
        return new ResponseResult(200,"删除成功");
    }

    @Override
    public ResponseResult viewAnnounceById(String hiddenId) {
        return null;
    }

    @Override
    public ResponseResult findAllAnnounceIf1(HttpSession session) {
        QueryWrapper<Announce> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ann_status",1);

        List<Announce> announceList = announceMapper.selectList(queryWrapper);
        session.setAttribute("announceListIf1",announceList);
        return null;
    }

    @Override
    public ResponseResult setAnnStatus0(String hiddenId, HttpSession session) {
        Announce announce = new Announce();
        Date time = new Date();
        announce.setId(hiddenId);
        announce.setAnnStatus(0);
        announce.setAnnDate(time);
        int row = announceMapper.updateById(announce);
        if(row!=1)
        {
            return new ResponseResult(201,"移除失败");
        }
        return new ResponseResult(200,"移除成功");
    }

    @Override
    public ResponseResult setAnnStatus1(String hiddenId, HttpSession session) {
        Announce announce = new Announce();
        Date time = new Date();
        announce.setId(hiddenId);
        announce.setAnnStatus(1);
        announce.setAnnDate(time);
        int row = announceMapper.updateById(announce);
        if(row!=1)
        {
            return new ResponseResult(201,"设置失败");
        }
        return new ResponseResult(200,"设置成功");
    }
}
